/**
 * $Id$
 * @author mcanovas
 * @date   Mar 2, 2012 5:49:05 PM
 *
 * Copyright (C) 2012 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.ui.configuration;

import java.util.List;

import org.sk.gsync.app.mybatis.Folder;
import org.sk.gsync.app.mybatis.FolderService;
import org.sk.gsync.ui.context.AbstractContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class ConfigurationFrameContext extends AbstractContext {

    @Autowired
    @Qualifier("folderService")
    private FolderService folderService;

    /**
     * @see org.sk.gsync.ui.context.AbstractContext#save()
     */
    @Override
    public void save() {
        @SuppressWarnings("unchecked")
        List<Folder> folders =
            (List<Folder>) getValue(ConfigurationFrameFields.FOLDERS_TO_SYNC);
        for (Folder folder : folders) {
            if (!folderService.exists(folder)) {
                folderService.addNewFolder(folder.getPath());
            }
        }
    }

    /**
     * @see org.sk.gsync.ui.context.AbstractContext#load()
     */
    @Override
    public void load() {
        List<Folder> folders = folderService.getFolders();
        setValue(ConfigurationFrameFields.FOLDERS_TO_SYNC, folders);
    }

}
